<?php

namespace app\admin\model\article;

use app\admin\model\recommend\WapRelation;
use app\admin\model\recommend\WebRelation;
use think\Model;
use traits\model\SoftDelete;

class Article extends Model
{
	use softDelete;

    

    

    // 表名
    protected $name = 'article';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'add_time';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = ['label','content'];
    



	public function getLabelAttr($value, $data){
		$value = json_decode($value ?? ($data['label'] ?? ''), true);
		return implode(',',$value);
	}

	protected function setLabelAttr($value, $data){
		return json_encode(explode(',',$value));
	}


    public function category()
    {
        return $this->hasOne('Category', 'id', 'cid', [], 'LEFT')->setEagerlyType(0);
    }

	public function content()
    {
        return $this->hasOne('Content', 'nid', 'id');
    }

	public function waprecom()
	{
		return $this->hasOne(WapRelation::class, 'link_id', 'id')->where('type',1);
	}

	public function webrecom()
	{
		return $this->hasOne(WebRelation::class, 'link_id', 'id')->where('type', 4);
	}
}
